<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改标签')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-label-edit" th:object="${label}">
            <input name="labelId" type="hidden" th:field="*{labelId}" />
            <input id="treeId" name="parentId" type="hidden" th:field="*{parentId}" />
            <input name="parentName" type="hidden" th:field="*{parentName}">
            <div class="form-group">
                <label class="col-sm-3 control-label">上级标签：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input class="form-control" type="text" id="treeName" onclick="selectLabelTree()" readonly="true" th:value="${label.parentName == null ? '无' : label.parentName}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">标签名称：</label>
                <div class="col-sm-8">
                    <input class="form-control" type="text" name="labelName" th:field="*{labelName}" id="labelName" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">显示排序：</label>
                <div class="col-sm-8">
                    <input class="form-control" type="text" name="orderNum" th:field="*{orderNum}" required>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script type="text/javascript">
        var prefix = ctx + "control/label";

        $("#form-label-edit").validate({
            onkeyup: false,
            rules:{
                labelName:{
                    remote: {
                        url: prefix + "/checkLabelNameUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "labelId": function() {
                                return $("#labelId").val();
                            },
                            "parentId": function() {
                                return $("input[name='parentId']").val();
                            },
                            "labelName": function() {
                                return $.common.trim($("#labelName").val());
                            },
                            "token" :　storage.get("token")
                        },
                        dataFilter: function(data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },
                orderNum:{
                    digits:true
                },
            },
            messages: {
                "labelName": {
                    remote: "标签已经存在"
                }
            },
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-label-edit').serialize());
            }
        }
        /*标签管理-修改-选择父源点树*/
            function selectLabelTree() {
            var deptId = $("#treeId").val();
            if(deptId > 0) {
                var options = {
                    title: '部门选择',
                    width: "380",
                    url: prefix + "/selectLabelTree/" + $("#treeId").val(),
                    callBack: doSubmit
                };
                $.modal.openOptions(options);
            } else {
                $.modal.alertError("根源点不能选择");
            }
        }
        function doSubmit(index, layero){
            var tree = layero.find("iframe")[0].contentWindow.$._tree;
            if ($.tree.notAllowLastLevel(tree)) {
                var body = layer.getChildFrame('body', index);
                $("#treeId").val(body.find('#treeId').val());
                $("#treeName").val(body.find('#treeName').val());
                $('input[name="parentName"]').val(body.find('#treeName').val());
                layer.close(index);
            }
        }
    </script>
</body>
</html>
